<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/21
 * Time: 16:48
 */

class operate extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        parent::checkLogin();

    }

    //在线玩家列表
    public function playerOnlineList()
    {

        $array = array();
//        $array = array("invalid" => 0);
        $result = $this->db
            ->select('id,isLogin,uid,bb.NickName,RoomCard,RegisterDate,OnLineTimeCount,win_number,lose_number,room_id,gid,total_buy_card tbc,total_cost_card tcc,total_buy_money tbm')
            ->from('sys_user aa')
            ->join('QPAccountsDB.dbo.AccountsInfo bb', 'aa.uid = bb.USERID', 'left')
            ->where($array)
            ->get()
            ->result_array();
        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('playeronlinelist.html');
        $this->load->view('footer.html');

    }

    // 在线玩家下次无法登陆

    public function doOnlineLogin()
    {
        $id = $this->uri->segment(3);
        $type = $this->uri->segment(4);
        $isLogin = 0;
        if ($type == 'no') {
            $isLogin = 1;
        }
        $data = array(
            'isLogin' => $isLogin,
        );
        $ret = $this->db->where('id', $id)->update('sys_user', $data);
        if ($ret) {
            $this->alert_message(1, '操作成功', site_url('operate/playerOnlineList'));
        } else {
            $this->alert_message(0, '操作失败', site_url('operate/playerOnlineList'));
        }
    }

    public function userBlackList()
    {
        $array = array('isBlack' => 1);
//        $array = array("invalid" => 0);
        $result = $this->db
            ->select('id,uid,bb.NickName,blacklist_time,RoomCard,RegisterDate,blacklist_time,gid,total_buy_card tbc,total_cost_card tcc,total_buy_money tbm')
            ->from('sys_user aa')
            ->join('QPAccountsDB.dbo.AccountsInfo bb', 'aa.uid = bb.USERID', 'left')
            ->where($array)
            ->get()
            ->result_array();
        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('userblacklist.html');
        $this->load->view('footer.html');
    }

    public function doCustomAddBlackList()
    {
        $gid = $this->input->post('gid');
        $uid = $this->input->post('uid');
        $drcp = $this->input->post('drcp');
        $type = $this->input->post('type');

//type 1 玩家 2 代理
        if ($type == 1) {
            $DB2 = $this->load->database('account', true);
            $array = array('UserID' => $uid);
            $ret = $DB2
                ->select('RoomCard as cardNumberBefore,NickName as nickname,Accounts as account')
                ->where($array)
                ->get('AccountsInfo')->result_array();
            if (!$ret) {
                $this->alert_message(0, '玩家不存在,请先注册', site_url('user/playerlist'));
                exit;
            }
        } else if ($type == 2) {
            $array = array(
                'id' => $uid,
            );
            $ret = $this->db
                ->where($array)->get('Record_Member')->result_array();
            if (!$ret) {
                $this->alert_message(0, '代理不存在,请先添加', site_url('user/agentlist'));
                exit;
            }
        }
        $res = $ret[0]['isBlack'];
//        $arr = array(
//            'uaid' => $uid,
//            'type' => $type
//        );
//
//        $res = $this->db
//            ->where($arr)->get('sys_blacklist')->result_array();

        if ($res) {
            $this->alert_message(0, '该账号已在黑名单,请勿重复添加', site_url('operate/userBlackList'));
            exit;
        }
        $data = array(
            'gid' => $gid,
            'time' => time(),
            'aid' => $_SESSION['adminId'],
            'drcp' => $drcp,
            'uaid' => $uid,
            'invalid' => 1,
            'type' => $type
        );
        $re = $this->db->insert('sys_blacklist', $data);

        $data = array(
            'isBlack' => 1,
            'blacklist_time' => time()
        );
        if ($type == 1) {
            $ret = $this->db->where('uid', $uid)->update('sys_user', $data);
        } else {
            $ret = $this->db->where('id', $uid)->update('Record_Member', $data);
        }
        if ($re && $ret) {
            if ($type == 1) {
                $this->alert_message(1, '黑名单添加成功', site_url('operate/userBlackList'));
            } else {
                $this->alert_message(1, '黑名单添加成功', site_url('operate/agentBlackList'));
            }
        } else {
            if ($type == 1) {
                $this->alert_message(0, '黑名单添加失败', site_url('operate/userBlackList'));
            } else {
                $this->alert_message(0, '黑名单添加失败', site_url('operate/agentBlackList'));
            }
        }
    }

//游戏监测
    public function gameList()
    {

        $array = array();
//        $array = array("invalid" => 0);
        $result = $this->db
            ->select('')
            ->from('sys_game_list')
            ->where($array)
            ->get()
            ->result_array();
        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('customgamelist.html');
        $this->load->view('footer.html');
    }

//代理黑名单

    public function agentBlackList()
    {
        $array = array('isBlack' => 1);
//        $array = array("invalid" => 0);
        $result = $this->db
            ->select('id,nickname,address,cardcount,total_buy_card tbc,total_buy_money tbm,total_return_card trc,total_add_card tac,total_cost_card tcc,gid,regtime,blacklist_time')
            ->from('Record_Member')
            ->where($array)
            ->get()
            ->result_array();
        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('agentblacklist.html');
        $this->load->view('footer.html');
    }

//恢复黑名单
    public function clearBlackList()
    {
        $id = $this->uri->segment(3);
        $type = $this->uri->segment(4);

        $array = array('isBlack' => 0);

        if ($type == 'agent') {
            $ret = $this->db->where('id', $id)->update('Record_Member', $array);
        } else {
            $ret = $this->db->where('uid', $id)->update('sys_user', $array);
        }

        if ($ret) {
            if ($type == 'agent') {
                $this->alert_message(1, '恢复成功', site_url('operate/agentBlackList'));
            } else {
                $this->alert_message(1, '恢复成功', site_url('operate/userBlackList'));
            }

        } else {
            if ($type == 'agent') {
                $this->alert_message(0, '恢复失败', site_url('operate/agentBlackList'));
            } else {
                $this->alert_message(0, '恢复失败', site_url('operate/userBlackList'));
            }
        }
    }

//代理数据监测
    public function agentInfo()
    {
        $array = array();
//        $array = array("invalid" => 0);
        $result = $this->db
            ->select('id,nickname,address,cardcount,total_buy_card tbc,total_buy_money tbm,total_return_card trc,total_add_card tac,total_cost_card tcc,gid,regtime,blacklist_time')
            ->from('Record_Member')
            ->where($array)
            ->get()
            ->result_array();


        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('agentinfolist.html');
        $this->load->view('footer.html');
    }

}